package com.quarkus.demo;

import com.demo.core.entity.Person;
import com.demo.core.entity.R;
import com.demo.core.service.DbService;
import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import org.jboss.resteasy.reactive.RestPath;

@Path("/db")
@Produces(MediaType.APPLICATION_JSON)
public class DbResource {

    @Inject
    DbService dbService;

    @GET
    @Path("/save")
    public R<Long> save(@QueryParam("name") String name,@QueryParam("gender")  Integer gender){
        Person person = new Person();
        person.setName(name);
        person.setGender(gender);
        return R.ok(dbService.save(person));
    }

    @GET
    @Path("/{id}")
    public R<Person> get(@RestPath Long id){
        return R.ok(dbService.getById(id));
    }

    @GET
    @Path("/remove/{id}")
    public R<Boolean> remove(@RestPath Long id){
        return R.ok(dbService.remove(id));
    }

    @GET
    @Path("/transaction")
    public R<Boolean> testTransaction(@QueryParam("name") String name,@QueryParam("gender")  Integer gender){
        Person person = new Person();
        person.setName(name);
        person.setGender(gender);
        return R.ok(dbService.testTransaction(person));
    }
}
